package order.servlet;

import lombok.SneakyThrows;
import order.dao.foodDao;
import order.dao.managerDao;
import order.pojo.food_information;
import order.pojo.manager;
import order.utils.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;

public class foodServlet  extends HttpServlet {
    DbUtil dbUtil = new DbUtil();
   foodDao foodDao=new foodDao();

    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();

        String action = req.getParameter("action");
        if("querry".equals(action)) {
            querryAllFood(req,resp);
            return;
        } else if("delete".equals(action)){
            deleteFoodById(req,resp);
            return;
        } else if("add".equals(action)){
            addFood(req,resp);
            return;
        } else if("update".equals(action)){
            updateFood(req,resp);
            return;
        }
    }

    @SneakyThrows
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {



        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();

        String action = req.getParameter("action");
        if("querry".equals(action)) {
            querryAllFood(req,resp);
            return;
        } else if("delete".equals(action)){
   deleteFoodById(req,resp);
            return;
        } else if("add".equals(action)){
     addFood(req,resp);
            return;
        } else if("update".equals(action)){
          updateFood(req,resp);
            return;
        }

    }


    //显示所有顾客
    private void querryAllFood(HttpServletRequest req,HttpServletResponse resp){
        Connection con = null;

        try {
            System.out.println("a=>进入了manager的querry的servlet");
            con = dbUtil.getCon();

            List<food_information> list = foodDao.querrAllFood(con);
            System.out.println("=>"+list.toString());
            req.setAttribute("listfood", list);
            System.out.println("a=>list"+list.toString());
//                   resp.sendRedirect("admin/managerInfo.jsp");
            req.getRequestDispatcher("manager/food.jsp").forward(req,resp);
            System.out.println("=>跳转完成");


        } catch (Exception e) {

        } finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }

    private void deleteFoodById(HttpServletRequest req,HttpServletResponse resp)  {
        System.out.println("=>进入了删除servlet");
        String foodId=req.getParameter("foodId");
        Connection con=null;
        try{
            con=dbUtil.getCon();

            foodDao.deleteFoodById(con,Integer.valueOf(foodId));
            resp.sendRedirect("foodr?action=querry");
//            req.getRequestDispatcher("manger?action=querry").forward(req,resp);
            System.out.println("=>完成了请求的转发");

        }catch (Exception e){

        }finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }
    //修改
    private void updateFood(HttpServletRequest req,HttpServletResponse resp) throws Exception {
        System.out.println("=>进入了修改servlet");
        food_information food=null;
        String UpfoodId=req.getParameter("UpfoodId");

        String foodPublisher=req.getParameter("food_publisher");


        Connection con=null;
        con=dbUtil.getCon();
        foodDao.updateFoodById(con,"1",Integer.valueOf(UpfoodId));

        req.getRequestDispatcher("food?action=querry").forward(req,resp);

    }
    //增加
    private void addFood(HttpServletRequest req,HttpServletResponse resp) throws Exception {


        System.out.println("=>进入了添加的servlet");
        food_information food=null;
        String foodId=req.getParameter("food_id");
        String foodName=req.getParameter("food_name");
        String foodPrice=req.getParameter("food_price");
        String foodNumber=req.getParameter("food_number");
        String storeid=req.getParameter("store_id");
        String foodImg=req.getParameter("food_img");
        String foodPublisher=req.getParameter("food_publisher");
        food=new food_information(Integer.valueOf(foodId),Float.valueOf(foodPrice),foodName,foodNumber,Integer.valueOf(storeid),foodImg,foodPublisher);
        Connection con=null;
        con=dbUtil.getCon();
       foodDao.addFood(con,food);


        req.getRequestDispatcher("food?action=querry").forward(req,resp);

    }



}
